package com.shangma.cn.commons.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 开发人员: 辉哥真球帅
 * 联系方式: zhengzhoudaxuevip@163.com
 * 创建时间: 2021/10/13  11:27
 * 文件含义:
 */

public class CacheUtils {


    public static final long expireTime = 5; //分钟

    /**
     * 设置值
     */
    public static void setCache(String key, String value) {
        SpringContainerUtils.getBean(StringRedisTemplate.class).opsForValue().set(key, value);
    }

    /**
     * 默认时间的值
     */
    public static void setCacheDefaultTime(String key, String value) {
        SpringContainerUtils.getBean(StringRedisTemplate.class).opsForValue().set(key, value, expireTime, TimeUnit.MINUTES);
    }

    /**
     * 指定时间的值
     */

    public static void setCacheWithTime(String key, String value, long minutes) {
        SpringContainerUtils.getBean(StringRedisTemplate.class).opsForValue().set(key, value, minutes, TimeUnit.MINUTES);
    }

    /**
     * 获取值
     */

    public static String getCache(String key) {
        return SpringContainerUtils.getBean(StringRedisTemplate.class).opsForValue().get(key);
    }

    /*
      删除值
     */

    public static void deleteCache(String key) {
        SpringContainerUtils.getBean(StringRedisTemplate.class).delete(key);
    }

    /**
     * 判断key是否存在
     */

    public static boolean isExsitsKey(String key) {
        return SpringContainerUtils.getBean(StringRedisTemplate.class).hasKey(key);
    }
}
